var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var _a;
var ChessTypes;
(function (ChessTypes) {
    ChessTypes["CAR"] = "car";
    ChessTypes["HORSE"] = "horse";
    ChessTypes["CANNON"] = "cannon";
    ChessTypes["ELEPHANT"] = "elephant";
    ChessTypes["SCHOLAR"] = "scholar";
    ChessTypes["KING"] = "king";
    ChessTypes["SOLDIER"] = "soldier";
})(ChessTypes || (ChessTypes = {}));
var Chess = /** @class */ (function () {
    function Chess(x, y, camp, chessType) {
        this.camp = 0;
        this.curX = x;
        this.curY = y;
        this.camp = camp;
        this.chessType = chessType;
        this.name = chessMap[chessType]["name_".concat(camp)];
    }
    Chess.prototype.move = function (x, y) {
        var _a;
        // 是否超出边界
        if (!this.isRange(x, y))
            return false;
        // 是否有己方
        if (((_a = checkerboard[y][x]) === null || _a === void 0 ? void 0 : _a.camp) === this.camp)
            return false;
        // 是否可移动
        var movePoints = this.getShowMovePoint();
        var hasMove = movePoints.some(function (item) { return item.x === x && item.y === y; });
        if (!hasMove)
            return false;
        // 是否有敌方
        var from = checkerboard[this.curY][this.curX];
        checkerboard[this.curY][this.curX] = null;
        checkerboard[y][x] = from;
        this.curX = x;
        this.curY = y;
        return true;
    };
    // 是否在边界
    Chess.prototype.isRange = function (x, y) {
        if (x < 0 || x > RANGE_X - 1) {
            return false;
        }
        if (y < 0 || y > RANGE_Y - 1) {
            return false;
        }
        return true;
    };
    // 获取周围棋子
    Chess.prototype.getAroundChess = function (x, y) {
        var _a, _b, _c, _d, _e, _f;
        if (!this.isRange(x, y))
            return {};
        var top = (_a = checkerboard[y - 1]) === null || _a === void 0 ? void 0 : _a[x];
        var bottom = (_b = checkerboard[y + 1]) === null || _b === void 0 ? void 0 : _b[x];
        var left = checkerboard[y][x - 1];
        var right = checkerboard[y][x + 1];
        var leftTop = (_c = checkerboard[y - 1]) === null || _c === void 0 ? void 0 : _c[x - 1];
        var leftBottom = (_d = checkerboard[y - 1]) === null || _d === void 0 ? void 0 : _d[x + 1];
        var rightTop = (_e = checkerboard[y + 1]) === null || _e === void 0 ? void 0 : _e[x - 1];
        var rightBottom = (_f = checkerboard[y + 1]) === null || _f === void 0 ? void 0 : _f[x + 1];
        return {
            top: top,
            bottom: bottom,
            left: left,
            right: right,
            leftTop: leftTop,
            leftBottom: leftBottom,
            rightTop: rightTop,
            rightBottom: rightBottom,
        };
    };
    // 获取x方向上下一个棋子
    Chess.prototype.getNextChessX = function (x, y) {
        var _a, _b;
        var curRow = checkerboard[y];
        var min = -1;
        var max = -1;
        for (var i = 0; i < curRow.length; i++) {
            if (i > x && ((_a = curRow[i]) === null || _a === void 0 ? void 0 : _a.chessType) && max === -1) {
                max = i;
            }
            else if (i < x && ((_b = curRow[i]) === null || _b === void 0 ? void 0 : _b.chessType)) {
                min = i;
            }
        }
        return {
            min: min,
            max: max,
        };
    };
    // 获取y方向上下一个棋子
    Chess.prototype.getNextChessY = function (x, y) {
        var min = -1;
        var max = -1;
        for (var i = 0; i < checkerboard.length; i++) {
            var chess = checkerboard[i][x];
            if (i > y && (chess === null || chess === void 0 ? void 0 : chess.chessType) && max === -1) {
                max = i;
            }
            else if (i < y && (chess === null || chess === void 0 ? void 0 : chess.chessType)) {
                min = i;
            }
        }
        return {
            min: min,
            max: max,
        };
    };
    Chess.prototype.getShowMovePoint = function () {
        var _this = this;
        var res = this.getMovePoint(this.curX, this.curY);
        // 去除己方和当前位置
        res = res
            .filter(function (v) { return _this.isRange(v.x, v.y); })
            .filter(function (v) {
            var _a;
            if (((_a = checkerboard[v.y][v.x]) === null || _a === void 0 ? void 0 : _a.camp) === _this.camp) {
                // 己方单位
                return false;
            }
            if (v.x === _this.curX && v.y === _this.curY) {
                return false;
            }
            return true;
        });
        var newPoint = [];
        var _loop_1 = function (item) {
            if (newPoint.find(function (v) { return item.x === v.x && item.y === v.y; })) {
                return "continue";
            }
            newPoint.push(item);
        };
        // 去重
        for (var _i = 0, res_1 = res; _i < res_1.length; _i++) {
            var item = res_1[_i];
            _loop_1(item);
        }
        return newPoint;
    };
    return Chess;
}());
var CarChess = /** @class */ (function (_super) {
    __extends(CarChess, _super);
    function CarChess(x, y, camp) {
        return _super.call(this, x, y, camp, ChessTypes.CAR) || this;
    }
    CarChess.prototype.getMovePoint = function (x, y) {
        var res = [];
        var nextChessX = this.getNextChessX(x, y);
        var nextChessY = this.getNextChessY(x, y);
        if (nextChessX.min >= nextChessX.max) {
            nextChessX.max = RANGE_X - 1 - nextChessX.max;
        }
        if (nextChessY.min >= nextChessY.max) {
            nextChessY.max = RANGE_Y - 1 - nextChessY.max;
        }
        for (var x_1 = nextChessX.min; x_1 <= nextChessX.max; x_1++) {
            res.push({
                x: x_1,
                y: y,
            });
        }
        for (var y_1 = nextChessY.min; y_1 <= nextChessY.max; y_1++) {
            res.push({
                x: x,
                y: y_1,
            });
        }
        return res;
    };
    return CarChess;
}(Chess));
var HorseChess = /** @class */ (function (_super) {
    __extends(HorseChess, _super);
    function HorseChess(x, y, camp) {
        return _super.call(this, x, y, camp, ChessTypes.HORSE) || this;
    }
    // 显示可移动的点
    HorseChess.prototype.getMovePoint = function (x, y) {
        var _a, _b, _c, _d;
        var res = [];
        var aroundChess = this.getAroundChess(x, y);
        if (!((_a = aroundChess.top) === null || _a === void 0 ? void 0 : _a.chessType)) {
            res.push({
                x: this.curX - 1,
                y: this.curY - 2,
            }, {
                x: this.curX + 1,
                y: this.curY - 2,
            });
        }
        if (!((_b = aroundChess.bottom) === null || _b === void 0 ? void 0 : _b.chessType)) {
            res.push({
                x: this.curX - 1,
                y: this.curY + 2,
            }, {
                x: this.curX + 1,
                y: this.curY + 2,
            });
        }
        if (!((_c = aroundChess.left) === null || _c === void 0 ? void 0 : _c.chessType)) {
            res.push({
                x: this.curX - 2,
                y: this.curY - 1,
            }, {
                x: this.curX - 2,
                y: this.curY + 1,
            });
        }
        if (!((_d = aroundChess.right) === null || _d === void 0 ? void 0 : _d.chessType)) {
            res.push({
                x: this.curX + 2,
                y: this.curY - 1,
            }, {
                x: this.curX + 2,
                y: this.curY + 1,
            });
        }
        return res;
    };
    return HorseChess;
}(Chess));
var CannonChess = /** @class */ (function (_super) {
    __extends(CannonChess, _super);
    function CannonChess(x, y, camp) {
        return _super.call(this, x, y, camp, ChessTypes.CANNON) || this;
    }
    CannonChess.prototype.getMovePoint = function (x, y) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        var res = [];
        var nextChessX = this.getNextChessX(x, y);
        var nextChessY = this.getNextChessY(x, y);
        if (nextChessX.min >= nextChessX.max) {
            nextChessX.max = RANGE_X - 1 - nextChessX.max;
        }
        if (nextChessY.min >= nextChessY.max) {
            nextChessY.max = RANGE_Y - 1 - nextChessY.max;
        }
        for (var x_2 = nextChessX.min + 1; x_2 < nextChessX.max; x_2++) {
            res.push({
                x: x_2,
                y: y,
            });
        }
        for (var y_2 = nextChessY.min + 1; y_2 < nextChessY.max; y_2++) {
            res.push({
                x: x,
                y: y_2,
            });
        }
        // nextChessX nextChessY 为基准再次寻找下一个是否是敌方
        var xMin = this.getNextChessX(nextChessX.min, y).min;
        var xMax = this.getNextChessX(nextChessX.max, y).max;
        if (checkerboard[y][xMin] !== checkerboard[y][x] &&
            ((_a = checkerboard[y][xMin]) === null || _a === void 0 ? void 0 : _a.camp) !== this.camp) {
            res.push({
                x: xMin,
                y: y,
            });
        }
        if (checkerboard[y][xMax] !== checkerboard[y][x] &&
            ((_b = checkerboard[y][xMax]) === null || _b === void 0 ? void 0 : _b.camp) !== this.camp) {
            res.push({
                x: xMax,
                y: y,
            });
        }
        var yMin = this.getNextChessY(x, nextChessY.min).min;
        var yMax = this.getNextChessY(x, nextChessY.max).max;
        if (((_c = checkerboard[yMin]) === null || _c === void 0 ? void 0 : _c[x]) !== checkerboard[y][x] &&
            ((_e = (_d = checkerboard[yMin]) === null || _d === void 0 ? void 0 : _d[x]) === null || _e === void 0 ? void 0 : _e.camp) !== this.camp) {
            res.push({
                x: x,
                y: yMin,
            });
        }
        if (((_f = checkerboard[yMax]) === null || _f === void 0 ? void 0 : _f[x]) !== checkerboard[y][x] &&
            ((_h = (_g = checkerboard[yMax]) === null || _g === void 0 ? void 0 : _g[x]) === null || _h === void 0 ? void 0 : _h.camp) !== this.camp) {
            res.push({
                x: x,
                y: yMax,
            });
        }
        // for (const key in aroundChess) {
        //   const item = aroundChess[key as keyof typeof aroundChess];
        //   if (item) {
        //     res = res.filter((v) => v.x !== item.curX && v.y !== item.curY);
        //   }
        // }
        return res;
    };
    return CannonChess;
}(Chess));
var ElephantChess = /** @class */ (function (_super) {
    __extends(ElephantChess, _super);
    function ElephantChess(x, y, camp) {
        return _super.call(this, x, y, camp, ChessTypes.ELEPHANT) || this;
    }
    ElephantChess.prototype.getMovePoint = function (x, y) {
        var _a, _b, _c, _d;
        var res = [];
        var aroundChess = this.getAroundChess(x, y);
        if (!((_a = aroundChess.leftTop) === null || _a === void 0 ? void 0 : _a.chessType)) {
            res.push({
                x: this.curX - 2,
                y: this.curY - 2,
            });
        }
        if (!((_b = aroundChess.leftBottom) === null || _b === void 0 ? void 0 : _b.chessType)) {
            res.push({
                x: this.curX - 2,
                y: this.curY + 2,
            });
        }
        if (!((_c = aroundChess.rightTop) === null || _c === void 0 ? void 0 : _c.chessType)) {
            res.push({
                x: this.curX + 2,
                y: this.curY - 2,
            });
        }
        if (!((_d = aroundChess.rightBottom) === null || _d === void 0 ? void 0 : _d.chessType)) {
            res.push({
                x: this.curX + 2,
                y: this.curY + 2,
            });
        }
        return res;
    };
    return ElephantChess;
}(Chess));
var ScholarChess = /** @class */ (function (_super) {
    __extends(ScholarChess, _super);
    function ScholarChess(x, y, camp) {
        return _super.call(this, x, y, camp, ChessTypes.SCHOLAR) || this;
    }
    ScholarChess.prototype.getMovePoint = function (x, y) {
        var res = [];
        if (this.camp === 0 ? this.curY <= 2 : this.curY >= 7) {
            if (this.curX > 3) {
                res.push({
                    x: this.curX - 1,
                    y: this.curY === 7 ? -1 : this.curY - 1,
                }, {
                    x: this.curX - 1,
                    y: this.curY === 2 ? -1 : this.curY + 1,
                });
            }
            if (this.curX < 5) {
                res.push({
                    x: this.curX + 1,
                    y: this.curY === 7 ? -1 : this.curY - 1,
                }, {
                    x: this.curX + 1,
                    y: this.curY === 2 ? -1 : this.curY + 1,
                });
            }
        }
        return res;
    };
    return ScholarChess;
}(Chess));
var KingChess = /** @class */ (function (_super) {
    __extends(KingChess, _super);
    function KingChess(x, y, camp) {
        return _super.call(this, x, y, camp, ChessTypes.KING) || this;
    }
    KingChess.prototype.getMovePoint = function (x, y) {
        var res = [];
        if (this.camp === 0 ? this.curY > 0 : this.curY > 7) {
            res.push({
                x: this.curX,
                y: this.curY - 1,
            });
        }
        if (this.camp === 0 ? this.curY < 2 : this.curY > 6) {
            res.push({
                x: this.curX,
                y: this.curY + 1,
            });
        }
        if (this.curX > 3) {
            res.push({
                x: this.curX - 1,
                y: this.curY,
            });
        }
        if (this.curX < 5) {
            res.push({
                x: this.curX + 1,
                y: this.curY,
            });
        }
        return res;
    };
    return KingChess;
}(Chess));
var SoldierChess = /** @class */ (function (_super) {
    __extends(SoldierChess, _super);
    function SoldierChess(x, y, camp) {
        return _super.call(this, x, y, camp, ChessTypes.SOLDIER) || this;
    }
    SoldierChess.prototype.getMovePoint = function (x, y) {
        var res = [];
        if (this.camp === 1) {
            res.push({
                x: this.curX,
                y: this.curY - 1,
            });
        }
        if (this.camp === 0) {
            res.push({
                x: this.curX,
                y: this.curY + 1,
            });
        }
        if (this.camp === 0 ? this.curY > 4 : this.curY < 5) {
            res.push({
                x: this.curX - 1,
                y: this.curY,
            });
        }
        if (this.camp === 0 ? this.curY > 4 : this.curY < 5) {
            res.push({
                x: this.curX + 1,
                y: this.curY,
            });
        }
        return res;
    };
    return SoldierChess;
}(Chess));
// 范围常量
var RANGE_X = 9;
var RANGE_Y = 10;
var checkerboard = [];
// 0 黑 1 红
var chessMap = (_a = {},
    _a[ChessTypes.CAR] = {
        name_0: "車",
        name_1: "车",
        Constructor: CarChess,
    },
    _a[ChessTypes.HORSE] = {
        name_0: "馬",
        name_1: "马",
        Constructor: HorseChess,
    },
    _a[ChessTypes.CANNON] = {
        name_0: "砲",
        name_1: "炮",
        Constructor: CannonChess,
    },
    _a[ChessTypes.ELEPHANT] = {
        name_0: "象",
        name_1: "相",
        Constructor: ElephantChess,
    },
    _a[ChessTypes.SCHOLAR] = {
        name_0: "士",
        name_1: "仕",
        Constructor: ScholarChess,
    },
    _a[ChessTypes.KING] = {
        name_0: "将",
        name_1: "帥",
        Constructor: KingChess,
    },
    _a[ChessTypes.SOLDIER] = {
        name_0: "卒",
        name_1: "兵",
        Constructor: SoldierChess,
    },
    _a);
var Game = /** @class */ (function () {
    function Game(redUnlimited, isAddAi) {
        if (redUnlimited === void 0) { redUnlimited = false; }
        if (isAddAi === void 0) { isAddAi = true; }
        var _this = this;
        this.RANGE_X = 9; // 棋盘 x
        this.RANGE_Y = 10; // 棋盘 y
        this.chessSize = 30; // 棋子大小
        this.cellSize = 50; // 棋盘格子大小
        this.isGameOver = false; // 是否游戏结束
        this.currentOperator = 1; // 当前操作方
        this.aiCamp = 0; // 人机阵营
        this.selectedChess = null;
        this.container = null;
        this.tips = null;
        this.status = "";
        this.baseChess = [
            { x: 0, y: 0, camp: 0, type: ChessTypes.CAR },
            { x: 1, y: 0, camp: 0, type: ChessTypes.HORSE },
            { x: 2, y: 0, camp: 0, type: ChessTypes.ELEPHANT },
            { x: 3, y: 0, camp: 0, type: ChessTypes.SCHOLAR },
            { x: 4, y: 0, camp: 0, type: ChessTypes.KING },
            { x: 5, y: 0, camp: 0, type: ChessTypes.SCHOLAR },
            { x: 6, y: 0, camp: 0, type: ChessTypes.ELEPHANT },
            { x: 7, y: 0, camp: 0, type: ChessTypes.HORSE },
            { x: 8, y: 0, camp: 0, type: ChessTypes.CAR },
            { x: 1, y: 2, camp: 0, type: ChessTypes.CANNON },
            { x: 7, y: 2, camp: 0, type: ChessTypes.CANNON },
            { x: 0, y: 3, camp: 0, type: ChessTypes.SOLDIER },
            { x: 2, y: 3, camp: 0, type: ChessTypes.SOLDIER },
            { x: 4, y: 3, camp: 0, type: ChessTypes.SOLDIER },
            { x: 6, y: 3, camp: 0, type: ChessTypes.SOLDIER },
            { x: 8, y: 3, camp: 0, type: ChessTypes.SOLDIER },
            { x: 0, y: 6, camp: 1, type: ChessTypes.SOLDIER },
            { x: 2, y: 6, camp: 1, type: ChessTypes.SOLDIER },
            { x: 4, y: 6, camp: 1, type: ChessTypes.SOLDIER },
            { x: 6, y: 6, camp: 1, type: ChessTypes.SOLDIER },
            { x: 8, y: 6, camp: 1, type: ChessTypes.SOLDIER },
            { x: 1, y: 7, camp: 1, type: ChessTypes.CANNON },
            { x: 7, y: 7, camp: 1, type: ChessTypes.CANNON },
            { x: 0, y: 9, camp: 1, type: ChessTypes.CAR },
            { x: 1, y: 9, camp: 1, type: ChessTypes.HORSE },
            { x: 2, y: 9, camp: 1, type: ChessTypes.ELEPHANT },
            { x: 3, y: 9, camp: 1, type: ChessTypes.SCHOLAR },
            { x: 4, y: 9, camp: 1, type: ChessTypes.KING },
            { x: 5, y: 9, camp: 1, type: ChessTypes.SCHOLAR },
            { x: 6, y: 9, camp: 1, type: ChessTypes.ELEPHANT },
            { x: 7, y: 9, camp: 1, type: ChessTypes.HORSE },
            { x: 8, y: 9, camp: 1, type: ChessTypes.CAR },
        ];
        this.redUnlimited = false;
        this.isAddAi = true;
        this.currentChessCount = 0;
        this.redUnlimited = redUnlimited;
        this.isAddAi = isAddAi;
        checkerboard = new Array(this.RANGE_Y)
            .fill(0)
            .map(function (_) { return new Array(_this.RANGE_X).fill(null); });
        if (globalThis.document) {
            this.initDom();
        }
        this.resetCheckerboard();
        this.currentChessCount = this.baseChess.length;
    }
    Game.prototype.initDom = function () {
        var _this = this;
        var _a;
        this.container = document.querySelector(".container");
        this.tips = document.querySelector(".tips span");
        document.documentElement.style.setProperty("--chessSize", "".concat(this.chessSize, "px"));
        document.documentElement.style.setProperty("--cellSize", "".concat(this.cellSize, "px"));
        document.documentElement.style.setProperty("--x", this.RANGE_X.toString());
        document.documentElement.style.setProperty("--y", this.RANGE_Y.toString());
        (_a = this.container) === null || _a === void 0 ? void 0 : _a.addEventListener("click", function (e) {
            if (_this.isGameOver) {
                if (confirm("游戏结束, 是否重新开始?")) {
                    _this.resetCheckerboard();
                }
                else {
                    _this.resetCheckerboard();
                }
                return;
            }
            var el = e.target;
            _this.handleMove(el);
        });
    };
    Game.prototype.handleMove = function (el) {
        var _a, _b, _c, _d;
        var toX = parseInt(el.dataset.x);
        var toY = parseInt(el.dataset.y);
        // 点击不是棋子 或 点自己
        if (!el.classList.contains("inner") || el.classList.contains("selected")) {
            return;
        }
        // 没有选中棋子
        if (el.classList.contains("empty") && !this.selectedChess) {
            return;
        }
        // 选中 点击空白 移动
        if (el.classList.contains("empty") && this.selectedChess) {
            var fromX = parseInt(this.selectedChess.dataset.x);
            var fromY = parseInt(this.selectedChess.dataset.y);
            var chess = checkerboard[fromY][fromX];
            if (chess) {
                var isMove = chess.move(toX, toY);
                if (isMove) {
                    this.onMove(chess);
                }
                else {
                    this.onNotMove();
                }
            }
            return;
        }
        // 点击的不是空白 选中棋子
        if (!el.classList.contains("empty")) {
            if (this.selectedChess) {
                var fromX = parseInt(this.selectedChess.dataset.x);
                var fromY = parseInt(this.selectedChess.dataset.y);
                var toChess = checkerboard[toY][toX];
                var fromChess = checkerboard[fromY][fromX];
                if ((toChess === null || toChess === void 0 ? void 0 : toChess.camp) !== (fromChess === null || fromChess === void 0 ? void 0 : fromChess.camp)) {
                    var isMove = fromChess === null || fromChess === void 0 ? void 0 : fromChess.move(toX, toY);
                    if (isMove) {
                        this.onMove(fromChess);
                        return;
                    }
                    else {
                        this.onNotMove();
                    }
                }
            }
            (_a = this.selectedChess) === null || _a === void 0 ? void 0 : _a.classList.remove("selected");
            if (this.currentOperator === ((_c = (_b = checkerboard[toY]) === null || _b === void 0 ? void 0 : _b[toX]) === null || _c === void 0 ? void 0 : _c.camp)) {
                el.classList.add("selected");
                this.onSelect((_d = checkerboard[toY]) === null || _d === void 0 ? void 0 : _d[toX]);
                this.selectedChess = el;
            }
        }
    };
    Game.prototype.resetCheckerboard = function () {
        checkerboard = checkerboard.map(function (v) { return v.map(function (v) { return null; }); });
        this.isGameOver = false;
        this.currentOperator = 1;
        this.status = "GAME_RUNNING";
        for (var _i = 0, _a = this.baseChess; _i < _a.length; _i++) {
            var bc = _a[_i];
            var x = bc.x, y = bc.y, camp = bc.camp, type = bc.type;
            var Constructor = chessMap[type].Constructor;
            var chess = new Constructor(x, y, camp);
            checkerboard[y][x] = chess;
        }
        this.draw();
    };
    Game.prototype.draw = function () {
        if (!this.container)
            return;
        this.container.innerHTML = "";
        for (var i = 0; i < checkerboard.length; i++) {
            var row = checkerboard[i];
            for (var j = 0; j < row.length; j++) {
                var chess = row[j];
                var item = document.createElement("div");
                var inner = document.createElement("div");
                item.appendChild(inner);
                item.classList.add("item");
                inner.classList.add("inner");
                if (i === RANGE_Y - 1 || j === RANGE_X - 1) {
                    item.classList.add("not_border");
                }
                if (i === 4) {
                    if (j === 0 || j === RANGE_X - 1) {
                        item.classList.add("range");
                    }
                    else {
                        item.classList.add("split");
                    }
                }
                this.container.appendChild(item);
                inner.dataset.x = j.toString();
                inner.dataset.y = i.toString();
                if (chess) {
                    inner.classList.add(chess.camp === 0 ? "black" : "red");
                    inner.textContent = chess.name;
                }
                else {
                    inner.classList.add("empty");
                }
            }
        }
    };
    Game.prototype.onMove = function (chess) {
        var _this = this;
        if (this.selectedChess) {
            this.selectedChess.classList.remove("selected");
        }
        var chesses = checkerboard.flat(2).filter(Boolean).length;
        if (chesses !== this.currentChessCount) {
            // 棋子被吃掉了
            this.currentChessCount = chesses;
            this.onEatChess();
        }
        this.draw();
        this.selectedChess = null;
        if (this.redUnlimited) {
            this.currentOperator = 1;
        }
        else {
            this.currentOperator = +!this.currentOperator;
        }
        this.tips.textContent = this.currentOperator ? "红方" : "黑方";
        this.tips.style.color = this.currentOperator ? "#f00" : "#000";
        document.querySelectorAll(".can_move").forEach(function (el) {
            el.classList.remove(".can_move");
        });
        requestAnimationFrame(function () {
            requestAnimationFrame(function () {
                if (_this.isWin()) {
                }
                else {
                    _this.isGeneral(chess);
                    _this.isAddAi &&
                        _this.aiCamp === _this.currentOperator &&
                        _this.startAi();
                }
            });
        });
    };
    Game.prototype.onEatChess = function () {
        this.showTips("吃", "warning");
    };
    // 是否将军 chess 刚刚移动的棋子
    Game.prototype.isGeneral = function (chess) {
        var _a, _b;
        if (!chess)
            return;
        var nextMovePoint = chess.getShowMovePoint(); // 获取下一次可移动的范围
        for (var _i = 0, nextMovePoint_1 = nextMovePoint; _i < nextMovePoint_1.length; _i++) {
            var point = nextMovePoint_1[_i];
            var x = point.x, y = point.y;
            if (((_b = (_a = checkerboard[y]) === null || _a === void 0 ? void 0 : _a[x]) === null || _b === void 0 ? void 0 : _b.chessType) === ChessTypes.KING) {
                // 存在将军
                this.status = chess.camp === 0 ? "BLACK_CHECK" : "RED_CHECK";
                this.showTips("将军", "warning");
                return;
            }
        }
    };
    Game.prototype.isWin = function () {
        var kings = checkerboard
            .flat(2)
            .filter(function (v) { return (v === null || v === void 0 ? void 0 : v.chessType) === ChessTypes.KING; });
        if (kings.length !== 2) {
            var black = kings.find(function (v) { return (v === null || v === void 0 ? void 0 : v.camp) === 0; });
            this.isGameOver = true;
            this.status = "GAME_OVER";
            if (!black) {
                this.showTips("红方胜利", "success");
            }
            else {
                this.showTips("黑方胜利", "success");
            }
            return true;
        }
        return false;
    };
    Game.prototype.startAi = function (camp) {
        var _this = this;
        if (camp === void 0) { camp = 0; }
        var chesses = checkerboard.flat(2).filter(function (v) { return (v === null || v === void 0 ? void 0 : v.camp) === camp; });
        var move = function () {
            var _a;
            var index = Math.floor(Math.random() * chesses.length);
            var moveRange = chesses[index].getShowMovePoint();
            var moveIndex = Math.floor(Math.random() * moveRange.length);
            var point = moveRange[moveIndex];
            if (!point) {
                move();
                return;
            }
            var isMove = (_a = chesses[index]) === null || _a === void 0 ? void 0 : _a.move(point.x, point.y);
            if (isMove) {
                _this.draw();
                _this.currentOperator = +!_this.currentOperator;
                _this.tips.textContent = _this.currentOperator ? "红方" : "黑方";
                _this.tips.style.color = _this.currentOperator ? "#f00" : "#000";
                return;
            }
            else {
                move();
            }
        };
        move();
    };
    Game.prototype.onNotMove = function () {
        this.showTips("不能执行此操作", "error");
    };
    Game.prototype.onSelect = function (chess) {
        if (chess) {
            document.querySelectorAll(".can_move").forEach(function (el) {
                el.classList.remove("can_move");
            });
            var moveRange = chess.getShowMovePoint();
            for (var _i = 0, moveRange_1 = moveRange; _i < moveRange_1.length; _i++) {
                var item = moveRange_1[_i];
                var x = item.x, y = item.y;
                var inner = document.querySelector(".inner[data-x=\"".concat(x, "\"][data-y=\"").concat(y, "\"]"));
                inner.classList.add("can_move");
            }
        }
    };
    Game.prototype.showTips = function (content, type) {
        if (type === void 0) { type = "success"; }
        var tips = document.createElement("div");
        tips.classList.add("game_tips", type);
        var text = document.createElement("div");
        text.textContent = content;
        tips.appendChild(text);
        document.body.appendChild(tips);
        tips.addEventListener("animationend", function () {
            tips.remove();
        });
    };
    return Game;
}());
new Game(false, false);
